MATH 105 LA

Homework: due Thursday 10/08/2015

Name:

Collaborators:

Please list the name of your collaborators.

You will be asked to fill the body of the functions. To make my life (and therefore yours) easier please respect the imput and output of every function. The homework will be graded using a script that test your functions (using the name, input and output I provided here) in a automated fashion.

Question 1: Numerical evaluation of $e^x$

You saw last time that trying to evaluate $e$, the Euler's number, by a well known limit didn't work well, due to finite precission arithmetics. In this question you will write a function to compute $e$ in a stable manner.

We will use a truncated Taylor series of $e^x$ around 0 which is given by

\begin{equation} e^{x} = \sum_{i = 0}^{\infty} \frac{x^i}{i!}. \end{equation}

Q1.a Write a small function that takes an integer nTerms, and gives back the approximation of $e$ via a truncated Taylor series with nTerms.


In [ ]:
function evalExponential(Nterms::Int)
    # complete the body of the function
    return approxE # return the approximation of e
end

Test your code by running


In [ ]:
evalExponential(10)-e

You should see that the result of your function converges fast as you increse the nTerms. Now run your code using the snippet below to observe the convergence rate. We first load the plotting library Gadfly.


In [ ]:
import Gadfly
using Gadfly

We compute the relative error for different number of terms and we plot it in semilog scale.


In [ ]:
Error = {abs(evalExponential(ii) -e)/e for ii=1:15} 
plot(x = 1:15, y = Error, Scale.y_log10)

Can you comment, based on the truncation error of the Taylor series, why you see this convergence ratio?

Answer:

Q1.b: In practice, we do not known the exact answer, so we want to know a priori how many terms we need to obtain certain accuracy. In this case you can easily check the theoretical accuracy using the reminder term in the truncated Taylor series. Write a function that computes the number of terms needed in the expansion to obtain a desired accuracy $\epsilon$.


In [ ]:
function numberTermsForAccuracy(ϵ)
    # complete the body of the function
    return nTerms # return the number of terms
end

Q1.c: Use your function numberTermsForAccuracy to evaluate $e$ using the truncated Taylor series to the desired accuracy.


In [ ]:
function evalExponential(ϵ::Float64)
    # complete the body of the function
    return approxE
end

Q1.d: Modify your function numberTermsForAccuracy to take any number $x$ and any accuracy $\epsilon$, and compute the number of terms needed to compute an approximation to $e^x$ using a truncated Taylor series.


In [ ]:
function numberTermsForAccuracy(x, ϵ)
    # complete the body of the function
    return nTerms
end

Q1.e: Write a function to evaluate $e^x$ for any accuracy $\epsilon$.


In [ ]:
function evalExponential(x,ϵ)
    # complete the body of the function
    return approxE
end

Question 2: Computing roots

Another important class of algorithms in scientific computing are the root-finding algorithms. They are central for solving algebraic equations, given that any algebraic equation can be translated to a root-finding problem.

for example solving the algebraic equation $x^3 = x +1$, can be translated to find the zeros of the function $f(x) = x^3 -x -1

Q2.a: You will implement the bisection method to find the root of any function $f$ within the interval $[a,b]$ with an accuracy $\epsilon$, and with maximun number of iterations $N_{\text{max}}$. Remember that by hypothesis $f(a)\cdot(b) <0$, i.e. the endpoints have different sign. The method should return a real number that it within $\epsilon$ distance from the root.


In [ ]:
function findRootBisection(f,a,b,ϵ, Nmax)
    # complete the body of the function
end

To test your function you can define a function, for example:


In [ ]:
function ff(x)
    return x^3+0.00001*x^4-0.3
end

And use your root finding algorithm!


In [ ]:
ff(findRootBisection(ff,-1.1,1.4,0.0000001, 30))

This should be really small.

Q2.b In the space below write a script that uses your root finding function to find the solution of the equation \begin{equation} 6(e^x -x) = 7 +3x^2 + 2x^3 \end{equation}

between $[-1,1]$ with a tolerance of $10^{-6}$.


In [ ]: